<template>
  <div class="app-container clearfix">  
  <el-row>
    <el-col :span="24">
      <div style="padding:25px;">
      今日舆情总数：{{feelingsTotal}}
      </div>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="9">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>舆情信息数走势</span>
            <div id="lineBarBox" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>        

      </div>
      
    </el-col>
    <el-col :span="5">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>情感分析</span>
            <div id="lineBarBoxb" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>     

      </div>   
      
    </el-col>
    <el-col :span="5">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>预警分布</span>
            <div id="lineBarBoxc" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>      

      </div>
      
    </el-col>
    <el-col :span="5">
      <div style="padding:15px;">
      <el-card shadow="always">
          <div slot="header" class="clearfix">
            <span>渠道来源</span>
            <div id="lineBarBoxd" class="chart" style="height:200px;width:100%;" />
          </div>
      </el-card>        

      </div>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="6">
       <el-radio-group v-model="filterBlock">
      <el-radio-button label="1">全部舆情</el-radio-button>
      <el-radio-button label="2">关键字</el-radio-button>
    </el-radio-group>
    </el-col>
    <el-col :span="11" style="text-algin:right;">
      <div v-if="filterBlock == '1'" style="padding:25px; text-align:right; line-height:36px;">
      统计周期：{{filterForm.beginDate}}
      </div>
    </el-col>
    <el-col :span="7">
      <div style="padding:25px 0;">
      <el-radio-group v-if="filterBlock == '1'" v-model="filterForm.scale">
        <el-radio-button label="0">今日</el-radio-button>
        <el-radio-button label="1">昨日</el-radio-button>
        <el-radio-button label="7">近7日</el-radio-button>
        <el-radio-button label="30">近30日</el-radio-button>
        <el-radio-button label="-1">自定义</el-radio-button>
      </el-radio-group>
      <el-button v-else @click="openSubscribeHidden =true">新关键字</el-button>
      </div>
    </el-col>
  </el-row>
  <el-row>
    <el-col :span="24">
      <el-table
      v-if="filterBlock == '1'"
      :data="documentationList"
      border
      style="width: 100%">
      <el-table-column
        prop="title"
        label="舆情信息">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="来源"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.originate.title }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="影响力"
        width="100">
      </el-table-column>
      <el-table-column
        prop="visitNum"
        label="传播度"
        width="100">
      </el-table-column>
      <el-table-column
        prop="publishTime"
        label="发布时间"
        width="200">
      </el-table-column>
      </el-table>
      <el-table
      v-if="filterBlock == '2'"
      :data="feelingsSubjectList"
      border
      style="width: 100%">
      <el-table-column
        label="标题">
          <template slot-scope="{row}">
            <span>{{ row.subjectName }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="类型">
          <template slot-scope="{row}">
            <span>{{ row.subjectType.label }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="有效期">
          <template slot-scope="{row}">
            <span>{{ row.beginTime }}-{{ row.endTime }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="订阅时间"
        width="200">
          <template slot-scope="{row}">
            <span>{{ row.subscribeTime }}</span>
          </template>
      </el-table-column>
      </el-table>

    </el-col>
  </el-row>

  
    <el-dialog
      title="关键字订阅"
      :visible.sync="openSubscribeHidden"
      :close-on-click-modal="false"
      width="30%">
      <el-form ref="filterForm" :model="feelingsSubjectForm"  label-position="left" label-width="100px">
        <el-form-item label="关键字" prop="subjectName">
          <el-input v-model="feelingsSubjectForm.subjectName" clearable placeholder="关键字" style="width: 200px;" class="filter-item"/>
        </el-form-item>
        <el-form-item label="类型" prop="subjectType">
          <el-select v-model="feelingsSubjectForm.subjectType" placeholder="请选择">
            <el-option
              v-for="item in subjectTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签" prop="keyword">
            <el-table
              key="subscribeProfileForm"
              :data="feelingsKeywordList"
              border
              fit
              highlight-current-row
              style="width: 100%;"
            >
              <el-table-column label="名称">
                <template slot-scope="{row}">
                  <el-input placeholder="名称" v-model="row.name"></el-input>
                </template>
              </el-table-column>
              <el-table-column label="" align="center" width="120" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                  <el-button v-if="feelingsKeywordList.length > 1" @click="handleSubscribeDelete(row)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          <el-button @click="handleSubscribeJoin()">
            新关键字
          </el-button>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="feelingsSubjectForm.remark" clearable placeholder="备注" style="width: 200px;" class="filter-item"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="openSubscribeHidden = false">取 消</el-button>
        <el-button type="primary" @click="onSubjectDataSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getSymbol} from '@/utils/symbol' // secondary package based on el-pagination
import {getWaringList} from '@/api/knowledge/document' 
import echarts from 'echarts'

const lineBars = document.getElementById('lineBarBox')
const animationDuration = 6000
export default {
  name: 'financeDashboard',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      documentationList:[],      
      feelingsSubjectList:[],
      socket:"",
      tableKey: 0,
      list: [],
      kbarChart:null,
      total: 0,
      listLoading: false,
      subjectTypeOptions:[
        {
          label:"品牌",
          value:1
        },
        {
          label:"艺人",
          value:2
        },
        {
          label:"公司",
          value:3
        },
        {
          label:"政府",
          value:4
        },
        {
          label:"产品",
          value:5
        },
        {
          label:"个体",
          value:6
        },
        {
          label:"行业",
          value:7
        }
      ],
      feelingsTotal:0,
      filterBlock:1, //1舆情，2关键字
      filterForm:{
        scale:'0',
        beginDate:"-",
        endDate:undefined
      },
      openSubscribeHidden:false,
      feelingsKeywordList:[{name:''}],
      feelingsSubjectForm:{
        keyword:[{name:''}]
      },
      feelingsFilterForm:{
        page:1,
        size:10
      },
      columnList:[
        {
          title:"大宗商品"
        },
        {
          title:"政策"
        },
        {
          title:"产业"
        }
      ],
      
      connected:"loading"
    }
  },
  mounted() {
    this.$nextTick(() => {
      //this.initChart()
    })
  },
  watch:{
    "filterBlock"(newVal){
      if(newVal == '2'){
          this.refreshSubjectList()
      }else{
         this.refreshDocumentationList()
      }
    },
    "filterForm.scale"(newVal,oldVal){
      newVal = parseInt(newVal)
      console.log(newVal)
      if(newVal >= 0){
        if(newVal == 0){
          //今日
          let dateHandle = new Date()
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        else if(newVal == 1){
          //昨日
          let dateHandle = new Date((new Date()).getTime()-60*60*24*1000)
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        else if(newVal == 7){
          //最近7日
          let dateHandle = new Date((new Date()).getTime()-60*60*24*7*1000)
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        else if(newVal == 30){
          //最近30日
          let dateHandle = new Date((new Date()).getTime()-60*60*24*30*1000)
          this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
        }
        let _dateHandle = new Date()
        this.filterForm.endDate = _dateHandle.getFullYear()+"-"+(_dateHandle.getMonth()+1)+"-"+_dateHandle.getDate()
        console.log(this.filterForm)
        this.refreshDocumentationList()
        
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    let symbolData = getSymbol()
    try {
      this.filterForm.localSymbol = symbolData.code
    } catch (error) {
      
    }
    let dateHandle = new Date()
    this.filterForm.beginDate = dateHandle.getFullYear()+"-"+(dateHandle.getMonth()+1)+"-"+dateHandle.getDate()
    this.refreshDocumentationList()
  },
  
  methods: {
    initChart() {
      this.chart = echarts.init(lineBars)

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: 'pageA',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageB',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [80, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageC',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [30, 52, 200, 334, 390, 330, 220],
          animationDuration
        }]
      })
    },
    open: function () {

        this.connected = 'loaded'

        this.getList()
    },
    error: function (e) {
        console.log("连接错误",JSON.stringify(e))
    },
    getMessage: function (receiveData) {
      receiveData = JSON.parse(receiveData.data)
        console.log(receiveData)
      if(receiveData.status != 200){
          console.log(receiveData.msg)
      }else{

        this.list = receiveData.data.list
      }
    },
    send: function (params) {
      params = JSON.stringify(params)
      this.socket.send(params)
    },
    close: function () {
        console.log("socket已经关闭")
    },
    getList:function(){
      this.send({
        "conversationType":"finance.renveue.getList",
        "target":{"id":"3"}
      })
    },
    refreshDocumentationList:function(){
      let _this = this
      getWaringList(_this.filterForm).then(res=>{
        if(res.status == 200){
          _this.documentationList = res.data.list;
          _this.feelingsTotal = res.data.total
        }
      });
    },
    refreshSubjectList:function(){
      let _this = this
      getSubjectList(_this.feelingsFilterForm).then(res=>{
        if(res.status == 200){
          _this.feelingsSubjectList = res.data.list;
          _this.feelingsTotal = res.data.total
          console.log(_this.feelingsSubjectList)
          console.log(_this.documentationList)
        }
      });
    },
    onSubjectDataSubmit:function(){
      let _this = this
      try {
        _this.feelingsSubjectForm.keyword = JSON.parse(_this.feelingsKeywordList)
      } catch (error) {
        _this.feelingsSubjectForm.keyword = JSON.stringify(_this.feelingsKeywordList)
      }
      console.log(_this.feelingsSubjectForm)
      submitSubjectData(_this.feelingsSubjectForm).then(res=>{
        if(res.status == 200){
          _this.openSubscribeHidden = false;
          _this.feelingsSubjectForm = {}
          _this.feelingsKeywordList = []
          console.log(_this.feelingsKeywordList)
          _this.refreshSubjectList()
        }else{
          alert(res.msg)
        }
      });
    },
    handleSubscribeDelete:function(row){
      let _this = this;
      _this.feelingsKeywordList.forEach((res,index)=>{
        if(res.name == row.name){
            _this.feelingsKeywordList.splice(index,1)
        }
      })
    },
    handleSubscribeJoin:function(){
      console.log(this.feelingsKeywordList)
      this.feelingsKeywordList.push({
        name:""
      })
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    }
  }
}
</script>
<style>
.filter { float: left; width: 160px; padding: 20px; min-height: 600px; }
.article { float: left; width: 800px; }
.article-item { width: 800px; height: 200px; margin-right: 25px; }
.article-item-text { float: left; width:600px; }
.article-item-icon { float: right; width:200px; height:150px; padding-left: 50px; }
.article-item-icon img { width:100%; height:100%; border-radius: 10px; }
</style>